package hu.ikti.gateway.impl;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 
 * @author andraskindler
 *
 * This class is responsible for creating jar files from the web service calls' bytestream
 * The only class variable is filepath, which is a string specifying the folder for the future jar files.
 */
public class ResourceManager {
	
	public static ResourceManager resourcemanager;
	
	private ResourceManager() {
	}
	
	public static ResourceManager getInstance() {
		if (resourcemanager == null) {
			synchronized (ResourceManager.class) {
				return resourcemanager = new ResourceManager();
			}
		} else
			return resourcemanager;
	}
	
	/**
	 * This method is responsible for creating a .jar file from a byte[], with the name specified in [name].
	 * @param in - the byte array containing the jar.
	 * @param name - the name of the file.
	 * @param extension - the extension of the file.
	 * @return returns the path to the recently created jar. It is null if the creation process encoutnered an error.
	 * @throws Exception 
	 */
	public String makeRes(byte[] in, String name, String type) throws Exception {
		String tempfilepath;
		tempfilepath = (type == "jar") ? WebserviceHandler.getInstance().BundlePath + name + ".jar"
				: WebserviceHandler.getInstance().ResPath + name;
		try {
			FileOutputStream fos = new FileOutputStream(tempfilepath);
			fos.write(in);
			fos.close();
			CustomLogService.info("File created: " + tempfilepath + " .");
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		return tempfilepath;
	}
	
}
